package com.weiaibenpao.demo.chislim.ui.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.bean.UserDataBean;
import com.weiaibenpao.demo.chislim.db.preferences.UserPreferences;
import com.weiaibenpao.demo.chislim.presenter.LoginPresenter;
import com.weiaibenpao.demo.chislim.utils.Constant;
import com.weiaibenpao.demo.chislim.view.LoginView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class NameSignChangeActivity extends BaseActivity<LoginView, LoginPresenter> implements LoginView {

    @BindView(R.id.inputET)
    EditText inputET;
    @BindView(R.id.clearText)
    ImageView clearText;
    @BindView(R.id.pointText)
    TextView pointText;
    @BindView(R.id.commitBtn)
    Button commitBtn;

    Intent intent;

    String TAG = "ljb";

    UserPreferences userPreferences;
    private int type;
    private int userId;

    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_name_sign_change;
    }
    public static void startSelf(Context context,int type){
        Intent intent = new Intent(context,NameSignChangeActivity.class);
        intent.putExtra(Constant.TYPE,type);
        context.startActivity(intent);
    }
    @Override
    public void init(Bundle savedInstanceState) {
        type = getIntent().getIntExtra(Constant.TYPE, 1);

        if(type==1){
            inputET.setHint("请输入新的昵称");
            pointText.setText("最多可输入10个字，不允许特殊字符");
        }else if(type==2){
            inputET.setHint("请输入新的签名");
            pointText.setText("最多可输入20个字，不允许特殊字符");
        }

        userPreferences = UserPreferences.getInstance();
        userId = userPreferences.getInt(UserPreferences.USER_ID);
        inputET.addTextChangedListener(mTextWatcher);

    }

    @OnClick({R.id.clearText, R.id.commitBtn,R.id.left_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_image:
               finish();
                break;
            case R.id.clearText:
                //清除已输入的
                inputET.setText("");
                break;
            case R.id.commitBtn:
                //提交数据
                if(!TextUtils.isEmpty(inputET.getText().toString())){
                    //TODO 上传
//                    userPreferences.getInt(UserPreferences.USER_ID)
//                    Toast.makeText(this,"上传",Toast.LENGTH_SHORT).show();
                    if(type==1){
                        mPresenter.upDataInfo(userId,"", inputET.getText().toString().trim(),"",0,0,"","");
                    }else if(type==2){
                        mPresenter.upDataInfo(userId,"",
                                "","",0,0,"",inputET.getText().toString().trim());
//                        mPresenter.updateUserSign(userPreferences.getInt(UserPreferences.USER_ID),inputET.getText().toString().trim());
                    }

                }else{
                    Toast.makeText(this,"" +
                            "",Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }

    TextWatcher mTextWatcher = new TextWatcher() {
        private CharSequence temp;
        private int editStart ;
        private int editEnd ;
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            temp = s;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
//          mTextView.setText(s);//将输入的内容实时显示
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            editStart = inputET.getSelectionStart();
            editEnd = inputET.getSelectionEnd();
            if(temp.length() == 0){
                commitBtn.setBackground(getResources().getDrawable(R.drawable.btn_shape_n));
            }else if(temp.length() > 0){

                commitBtn.setBackground(getResources().getDrawable(R.drawable.btn_shape));
            }else if (temp.length() > 10) {
                Toast.makeText(NameSignChangeActivity.this,
                        "你输入的字数已经超过了限制！", Toast.LENGTH_SHORT)
                        .show();
                s.delete(editStart-1, editEnd);
                int tempSelection = editStart;
                inputET.setText(s);
                inputET.setSelection(tempSelection);
            }
        }
    };



    @Override
    public LoginPresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void upDataInfoSuccess(UserBean userBean) {
        EventBus.getDefault().post(userBean);
        finish();
    }

    @Override
    public void loginSuccess(UserDataBean userDataBean) {

    }


    @Override
    public void seedSuccess(String str) {

    }



}
